package com.rentong.wx.cust.service.impl;

import com.rentong.wx.cust.dao.CustServiceOrderDao;
import com.rentong.wx.cust.dao.CustServiceOrderPingjiaDao;
import com.rentong.wx.cust.dao.WxCustUserMapper;
import com.rentong.wx.cust.domain.CustServiceOrderEntity;
import com.rentong.wx.cust.domain.CustServiceOrderPingjiaEntity;
import com.rentong.wx.cust.domain.WxCustUser;
import com.rentong.wx.cust.service.CustServiceOrderPingjiaService;
import com.vdurmont.emoji.EmojiParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class CustServiceOrderPingjiaServiceImpl implements CustServiceOrderPingjiaService {

    @Resource
    private CustServiceOrderPingjiaDao custServiceOrderPingjiaDao;

    @Autowired
    private CustServiceOrderDao custServiceOrderDao;

    @Autowired
    private WxCustUserMapper wxCustUserMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submitPingjia(CustServiceOrderPingjiaEntity custServiceOrderPingjia, Long serviceOrderId, Long custId) {
        custServiceOrderPingjia.setCustId(custId);
        custServiceOrderPingjia.setServiceOrderId(serviceOrderId);
        CustServiceOrderEntity custServiceOrderEntity = custServiceOrderDao.selectByPrimaryKey(serviceOrderId);
        custServiceOrderPingjia.setChefId(custServiceOrderEntity.getChefId());
        custServiceOrderPingjia.setAddTime(new Date());
        //去掉表情
        String pingjiaContent = EmojiParser.replaceAllEmojis(custServiceOrderPingjia.getPingjiaContent(), "");
        custServiceOrderPingjia.setPingjiaContent(pingjiaContent);
        custServiceOrderPingjiaDao.insert(custServiceOrderPingjia);

        //更新评价数和好评数
        wxCustUserMapper.updatePingJiaNum(custServiceOrderEntity.getChefId(),custServiceOrderPingjia.getStarNum()==5?1:0);
    }
}